package 文件IO.流InputStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) throws IOException {
        /*InputStream inputStream = null;
        try{
             inputStream = new FileInputStream("E:/test.jpg");//打开文件
            int count = 0;
            while(true){
                count++;
                byte[] bytes = new byte[2048*2];//作为输出一次读多少
                int n = inputStream.read(bytes);
                System.out.println("n = "+n);
                if(n == -1){
                    //读到了末尾
                    break;
                }
                for(int i = 0; i < n; i++){
                    System.out.printf("0x%x ",bytes[i]);
                }
                System.out.println();
            }
            System.out.println("count = "+count);
        }finally{
            inputStream.close();
        }*/
        //优雅写法
        try(InputStream inputStream = new FileInputStream("E:/test.jpg")){
            while(true){
                byte[] bytes = new byte[1024];
                int n = inputStream.read(bytes);
                System.out.println("n = "+n);
                if(n == -1){
                    break;
                }
                for(int i = 0; i < n; i++){
                    System.out.printf("0x%x ",bytes[i]);
                }
                System.out.println();
            }

        }
    }
}
